{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:43:48.745691Z",
     "start_time": "2025-01-07T13:43:48.740606Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# Numpy ufunc 函数，randn跟的是维数\n",
    "df = pd.DataFrame(np.random.randn(5,4) - 1) #生成5行4列的随机数据\n",
    "print(df)\n",
    "f = np.abs(df)\n",
    "print(np.abs(df)) #绝对值"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0  0.197784  0.796889 -1.276947 -1.731500\n",
      "1 -2.005651 -0.844881 -0.207805 -0.861762\n",
      "2 -0.146826 -0.572059 -2.905024 -1.578160\n",
      "3  0.229069 -0.944097 -0.768580 -2.410271\n",
      "4 -1.907028  1.484362 -1.152365 -0.832566\n",
      "          0         1         2         3\n",
      "0  0.197784  0.796889  1.276947  1.731500\n",
      "1  2.005651  0.844881  0.207805  0.861762\n",
      "2  0.146826  0.572059  2.905024  1.578160\n",
      "3  0.229069  0.944097  0.768580  2.410271\n",
      "4  1.907028  1.484362  1.152365  0.832566\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:44:27.508792Z",
     "start_time": "2025-01-07T13:44:27.503754Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#apply默认作用在列上,x是每一列,因为axis=0\n",
    "print(df.apply(lambda x : x.max()))#每一列的最大值\n",
    "print(df.apply(lambda x : x.min()))#每一列的最小值\n",
    "print(df.apply(lambda f : f.max()))"
   ],
   "id": "3855a4452e185e26",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.229069\n",
      "1    1.484362\n",
      "2   -0.207805\n",
      "3   -0.832566\n",
      "dtype: float64\n",
      "0   -2.005651\n",
      "1   -0.944097\n",
      "2   -2.905024\n",
      "3   -2.410271\n",
      "dtype: float64\n",
      "0    0.229069\n",
      "1    1.484362\n",
      "2   -0.207805\n",
      "3   -0.832566\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:44:39.579775Z",
     "start_time": "2025-01-07T13:44:39.575163Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#apply作用在行上\n",
    "print(df.apply(lambda x : x.max(), axis=1))#每一行的最大值"
   ],
   "id": "27bcc9613b0ec9e0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.796889\n",
      "1   -0.207805\n",
      "2   -0.146826\n",
      "3    0.229069\n",
      "4    1.484362\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:44:45.989048Z",
     "start_time": "2025-01-07T13:44:45.981391Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用applymap应用到每个数据\n",
    "print(df.map(lambda x : '%.2f' % x))\n",
    "df.dtypes"
   ],
   "id": "74edc5b495c2061c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2      3\n",
      "0   0.20   0.80  -1.28  -1.73\n",
      "1  -2.01  -0.84  -0.21  -0.86\n",
      "2  -0.15  -0.57  -2.91  -1.58\n",
      "3   0.23  -0.94  -0.77  -2.41\n",
      "4  -1.91   1.48  -1.15  -0.83\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0    float64\n",
       "1    float64\n",
       "2    float64\n",
       "3    float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:45:05.902082Z",
     "start_time": "2025-01-07T13:45:05.898712Z"
    }
   },
   "cell_type": "code",
   "source": "type('%.2f' % 1.3456)#将数字格式化为字符串",
   "id": "801d29acd1109a36",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 处理缺失数据\n",
   "id": "98a84642d74c6d4e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:46:16.925337Z",
     "start_time": "2025-01-07T13:46:16.920412Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_data = pd.DataFrame([np.random.randn(3), [1., 2., np.nan],\n",
    "                       [np.nan, 4., np.nan], [1., 2., 3.]])#生成含有缺失值的DataFrame\n",
    "print(df_data.head())"
   ],
   "id": "d924e000cb27007f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         0         1        2\n",
      "0  0.86894  0.158707  1.35406\n",
      "1  1.00000  2.000000      NaN\n",
      "2      NaN  4.000000      NaN\n",
      "3  1.00000  2.000000  3.00000\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "95659e67e32b3a33"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:46:36.143444Z",
     "start_time": "2025-01-07T13:46:36.139721Z"
    }
   },
   "cell_type": "code",
   "source": "df_data.iloc[2,0]#获取第3行第1列的数据",
   "id": "7001159d499fa488",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(nan)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:46:42.684139Z",
     "start_time": "2025-01-07T13:46:42.680067Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#isnull来判断是否有空的数据\n",
    "print(df_data.isnull())#判断是否为空"
   ],
   "id": "472a4c6e66dd56c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2\n",
      "0  False  False  False\n",
      "1  False  False   True\n",
      "2   True  False   True\n",
      "3  False  False  False\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:47:05.741116Z",
     "start_time": "2025-01-07T13:47:05.736369Z"
    }
   },
   "cell_type": "code",
   "source": "print(df_data.isnull().sum()/len(df_data))#计算每一列的缺失率",
   "id": "79820e3853bb6ea2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.25\n",
      "1    0.00\n",
      "2    0.50\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:47:56.894232Z",
     "start_time": "2025-01-07T13:47:56.889686Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#inplace True是修改的是原有的df\n",
    "#subset=[0]是指按第一列来删除,第一列有空值就删除对应的行\n",
    "print(df_data.dropna(subset=[0]))\n",
    "# df_data"
   ],
   "id": "b619340e0a6998d1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         0         1        2\n",
      "0  0.86894  0.158707  1.35406\n",
      "1  1.00000  2.000000      NaN\n",
      "3  1.00000  2.000000  3.00000\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:47:32.416027Z",
     "start_time": "2025-01-07T13:47:32.411313Z"
    }
   },
   "cell_type": "code",
   "source": "df_data#",
   "id": "a69efc0022851fbb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         0         1        2\n",
       "0  0.86894  0.158707  1.35406\n",
       "1  1.00000  2.000000      NaN\n",
       "2      NaN  4.000000      NaN\n",
       "3  1.00000  2.000000  3.00000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.86894</td>\n",
       "      <td>0.158707</td>\n",
       "      <td>1.35406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.00000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.00000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.00000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:48:11.833095Z",
     "start_time": "2025-01-07T13:48:11.828667Z"
    }
   },
   "cell_type": "code",
   "source": "print(df_data.dropna(axis=1))  #某列由nan就删除该列",
   "id": "3bffd85fb3f4a08d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          1\n",
      "0  0.158707\n",
      "1  2.000000\n",
      "2  4.000000\n",
      "3  2.000000\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:48:24.817390Z",
     "start_time": "2025-01-07T13:48:24.812573Z"
    }
   },
   "cell_type": "code",
   "source": "df_data #原来的df没有变化",
   "id": "16e09e33e6a5c858",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         0         1        2\n",
       "0  0.86894  0.158707  1.35406\n",
       "1  1.00000  2.000000      NaN\n",
       "2      NaN  4.000000      NaN\n",
       "3  1.00000  2.000000  3.00000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.86894</td>\n",
       "      <td>0.158707</td>\n",
       "      <td>1.35406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.00000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.00000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.00000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 填充缺失数据",
   "id": "5aa2455717f1225b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:49:21.857347Z",
     "start_time": "2025-01-07T13:49:21.851606Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#给零列的空值填为-100，按特征（按列）去填充\n",
    "print(df_data.iloc[:,0].fillna(-100.))#给第一列的空值填为-100\n",
    "df_data"
   ],
   "id": "c75227826c1fd0b4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      0.86894\n",
      "1      1.00000\n",
      "2   -100.00000\n",
      "3      1.00000\n",
      "Name: 0, dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "         0         1        2\n",
       "0  0.86894  0.158707  1.35406\n",
       "1  1.00000  2.000000      NaN\n",
       "2      NaN  4.000000      NaN\n",
       "3  1.00000  2.000000  3.00000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.86894</td>\n",
       "      <td>0.158707</td>\n",
       "      <td>1.35406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.00000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.00000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.00000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:49:34.553710Z",
     "start_time": "2025-01-07T13:49:34.549814Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#依次拿到每一列\n",
    "for i in df_data.columns:\n",
    "    print(df_data.loc[:,i])"
   ],
   "id": "f622d94255c483d9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.86894\n",
      "1    1.00000\n",
      "2        NaN\n",
      "3    1.00000\n",
      "Name: 0, dtype: float64\n",
      "0    0.158707\n",
      "1    2.000000\n",
      "2    4.000000\n",
      "3    2.000000\n",
      "Name: 1, dtype: float64\n",
      "0    1.35406\n",
      "1        NaN\n",
      "2        NaN\n",
      "3    3.00000\n",
      "Name: 2, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:49:48.066659Z",
     "start_time": "2025-01-07T13:49:48.062787Z"
    }
   },
   "cell_type": "code",
   "source": "df_data.iloc[:,2]=df_data.iloc[:,2].fillna(df_data.iloc[:,2].mean()) #用均值填充空值",
   "id": "d0b8d4665ec84529",
   "outputs": [],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:49:52.096787Z",
     "start_time": "2025-01-07T13:49:52.089673Z"
    }
   },
   "cell_type": "code",
   "source": "df_data",
   "id": "9e01908401ca4596",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         0         1        2\n",
       "0  0.86894  0.158707  1.35406\n",
       "1  1.00000  2.000000  2.17703\n",
       "2      NaN  4.000000  2.17703\n",
       "3  1.00000  2.000000  3.00000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.86894</td>\n",
       "      <td>0.158707</td>\n",
       "      <td>1.35406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.00000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>2.17703</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>2.17703</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.00000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.00000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
